package com.stellar.auth.controller.admin.authRole;

import com.google.common.base.Preconditions;
import com.stellar.auth.controller.admin.authRole.vo.AuthRoleRequestPageVO;
import com.stellar.auth.controller.admin.authRole.vo.AuthRoleRequestVO;
import com.stellar.auth.controller.admin.authRole.vo.AuthRoleResponseVO;
import com.stellar.auth.domain.authRole.AuthRoleDomainService;

import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.bean.Result;
import com.stellar.web.core.validator.ValidatorRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色表控制器
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-16 18:06:56
 */
@RequestMapping("/api-admin/authRole")
@RestController("adminAuthRoleController")
@Slf4j
public class AuthRoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 分页查询角色表
     */
    @ValidatorRole
    @PostMapping("/page")
    public Result<PageResult<AuthRoleResponseVO>> page(@RequestBody AuthRoleRequestPageVO requestPageVO) {
        return Result.success(authRoleDomainService.page(requestPageVO));
    }

    /**
     * 新增角色表以及权限关联
     */
    @ValidatorRole
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody AuthRoleRequestVO requestVO) {
        try {
            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestVO, "请求体不能为空");
                Preconditions.checkNotNull(requestVO.getRoleName(),"角色名不能为空");
                Preconditions.checkNotNull(requestVO.getRoleKey(),"角色标识不能为空");
            }

            return Result.success(authRoleDomainService.insert(requestVO));
        } catch (IllegalArgumentException iae) {
            log.error("AuthRoleController.insert.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }

    /**
     * 修改角色
     */
    @ValidatorRole
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleRequestVO requestVO) {
       try {
           if (log.isInfoEnabled()) {
               Preconditions.checkNotNull(requestVO, "请求体不能为空");
               Preconditions.checkNotNull(requestVO.getId(),"角色Id不能为空");
               Preconditions.checkNotNull(requestVO.getRoleName(),"角色名不能为空");
               Preconditions.checkNotNull(requestVO.getRoleKey(),"角色标识不能为空");
           }

           return Result.success(authRoleDomainService.update(requestVO));
       } catch (IllegalArgumentException iae) {
           log.error("AuthRoleController.update.error:{}", iae.getMessage());
           return Result.fail(iae.getMessage());
       }
    }

    /**
     * 删除角色
     */
    @ValidatorRole
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(authRoleDomainService.delete(id));
    }

    /**
     * 获取角色详情以及相关权限
     */
    @ValidatorRole
    @GetMapping("/get/{id}")
    public Result<AuthRoleResponseVO> get(@PathVariable("id") Long id) {
        return Result.success(authRoleDomainService.get(id));
    }

    /**
     * 获取角色列表
     */
    @ValidatorRole
    @GetMapping("/list")
    public Result<List<AuthRoleResponseVO>> list() {
        return Result.success(authRoleDomainService.list());
    }
}
